﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Chimp.Core;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace SpaceChimpIDE
{
    public partial class ChimpViewport : OpenTK.GLControl
    {
        
        public ChimpViewport ( )
        {
            InitializeComponent ( );
         
        }

        public static void ResetEngine()
        {
            
            ChimpApp.ResetGL();
            ChimpApp.ActiveState.Clear();
            
        }

        public void ResetChimp()
        {

            ChimpApp.ActiveState.Clear();

        }

        private static bool Created = false;
        protected override void OnResize ( EventArgs e )
        {
            if ( !Created ) return;
            ChimpApp.W = ClientSize.Width;
            ChimpApp.H = ClientSize.Height;
            ChimpApp.DoGL();
        }

        protected override void  OnMouseHover(EventArgs e)
        {
            ChimpUI.XI = 0;
            ChimpUI.YI = 0;
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            ChimpUI.XI = e.X - ChimpUI.MX;
            ChimpUI.YI = e.Y - ChimpUI.MY;
            ChimpUI.MX = e.X;
            ChimpUI.MY = e.Y;
          
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            switch(e.Button)
            {
                case MouseButtons.Left:
                    ChimpUI.BUT[0]=true;
                    break;
                case MouseButtons.Right:
                    ChimpUI.BUT[1] = true;
                    break;
            }
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            switch ( e.Button )
            {
                case MouseButtons.Left:
                    ChimpUI.BUT[0] = false;
                    break;
                case MouseButtons.Right:
                    ChimpUI.BUT[ 1 ] = false;
                    break;
            }
        }
            protected override void OnPaint ( PaintEventArgs e )
        {
            base.OnPaint(e);

            if (!this.DesignMode)
            {
                ChimpApp.W = ClientSize.Width;
                ChimpApp.H = ClientSize.Height;
            if (Created == false)
            {
                MakeCurrent();
                ResetEngine();
                Created = true;
            }
            if (!this.DesignMode)
            {
         
            }

            if (!this.DesignMode)
            {

                GL.Clear(ClearBufferMask.ColorBufferBit);
                if (ChimpApp.CurState != null) ChimpApp.CurState.OnDraw();
                foreach(var t in ChimpApp.TList)
                {
                    t.OnDraw();
                }
                SwapBuffers();

                ChimpUI.XI = 0;
                ChimpUI.YI = 0;
            }
        }
    }

        private void UpdateChimp()
        {
            if (ChimpApp.CurState != null) ChimpApp.CurState.OnUpdate();
            foreach(var t in ChimpApp.TList)
            {
                t.OnUpdate();
            }
            Refresh();
        }

        private void timer1_Tick ( object sender, EventArgs e )
        {
            UpdateChimp();
        }
    }

}
